package com.cci.kangdao.utilTool;

import com.github.pagehelper.util.StringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 商伟
 * @title: HTMLUtils
 * @projectName kangDaoSystem
 * @description: 过滤特殊字符
 * @date 2022/4/14 10:14
 */
public class HTMLUtils {

    /**
     * 过滤特殊字符
     * @param str
     * @return
     *
     * \u00A0 特殊的空格
     */
    public static String stringFilter (String str){
        String regEx="[#$&*|'<>]";
        Pattern p = Pattern.compile(regEx);
        if(StringUtil.isNotEmpty(str)){
            Matcher m = p.matcher(str);
            return m.replaceAll("").trim();
        }
       return str;
    }

    /**
     * 过滤所有HTML 标签
     * @param htmlStr
     * @return
     */
    public static String filterHTMLTag(String htmlStr) {
        //定义HTML标签的正则表达式
        String reg_html="<[^>]+>";
        Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        htmlStr=matcher.replaceAll(""); //过滤html标签
        return htmlStr;
    }
    /**
     * 过滤标签，通过标签名
     * @param htmlStr
     * @param tagName
     * @return
     */
    public static String filterTagByName(String htmlStr,String tagName) {
        String reg_html="<"+tagName+"[^>]*?>[\\s\\S]*?<\\/"+tagName+">";
        Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        htmlStr=matcher.replaceAll(""); //过滤html标签
        return htmlStr;
    }
    /**
     * 过滤标签上的 style 样式
     * @param htmlStr
     * @return
     */
    public static String filterHTMLTagInStyle(String htmlStr) {
        String reg_html="style=('|\")(.*?)('|\")";
        Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        htmlStr=matcher.replaceAll(""); //过滤html标签
        return htmlStr;
    }
    /**
     * 替换表情
     * @param htmlStr
     * @return
     */
    public static String replayFace(String htmlStr) {
        String reg_html="\\[em_\\d{1,}\\]";
        Pattern pattern = Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        if(matcher.find()) {
            matcher.reset();
            while(matcher.find()) {
                String num = matcher.group(0);
                String number=num.substring(num.lastIndexOf('_')+1, num.length()-1);
                htmlStr = htmlStr.replace(num, "<img src='/face/arclist/"+number+".gif' border='0' />");
            }
        }
        return htmlStr;
    }

}
